﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ExeInvoker
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                ProgramInfoViewModelManager.Instance.Load();
                this.RefreshPrograms();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "读取储存的程序信息失败", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void RefreshPrograms()
        {
            this.dgvPrograms.ItemsSource = null;
            this.dgvPrograms.ItemsSource = ProgramInfoViewModelManager.Instance.AllProgramInfoViewModels;
        }

        private ProgramInfoViewModel GetSelctedProgramInfo()
        {
            if (this.dgvPrograms.SelectedCells.Count < 1)
            {
                MessageBox.Show("请先选择程序信息");
                return null;
            }
            return this.dgvPrograms.SelectedCells[0].Item as ProgramInfoViewModel;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (new ProgramAddOrEdit().ShowDialog().Value)
                {
                    this.RefreshPrograms();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = this.GetSelctedProgramInfo();
                if (item == null)
                {
                    return;
                }
                if (MessageBox.Show("是否删除:" + item.ProgramPath + " \n" + item.ProgramArgs, "提示", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }
                ProgramInfoViewModelManager.Instance.Remove(item);
                ProgramInfoViewModelManager.Instance.Save();
                this.RefreshPrograms();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = this.GetSelctedProgramInfo();
                if (item == null)
                {
                    return;
                }
                if (new ProgramAddOrEdit { ProgramInfo = item }.ShowDialog().Value)
                {
                    this.RefreshPrograms();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExe_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.dgvPrograms.SelectedCells.Count < 0)
                {
                    MessageBox.Show("请选择要执行的程序");
                    return;
                }

                this.Execute(this.dgvPrograms.SelectedCells[0].Item as ProgramInfoViewModel);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                string key = e.Key.ToString();
                var item = ProgramInfoViewModelManager.Instance.AllProgramInfoViewModels.FirstOrDefault(obj => obj.StrokeKeys.Equals(key, StringComparison.OrdinalIgnoreCase));
                if (item == null)
                {
                    return;
                }

                this.Execute(item);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Execute(ProgramInfoViewModel item)
        {
            this.tbOutput.AppendText("程序执行开始:" + DateTime.Now.ToString() + Environment.NewLine+Environment.NewLine);
            string output = ProgramInfoViewModelManager.Instance.Execute(item);
            if (this.tbOutput.LineCount > 1000)
            {
                this.tbOutput.Text = "";
            }

            this.tbOutput.AppendText(output);
            this.tbOutput.AppendText("程序执行结束:" + DateTime.Now.ToString() + Environment.NewLine+Environment.NewLine);
            this.tbOutput.ScrollToEnd();
        }

        private void btnClearMessage_Click(object sender, RoutedEventArgs e)
        {
            this.tbOutput.Text = "";
        }
    }
}
